package oakclub.utility;

import java.io.InputStream;
import java.util.List;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static java.util.Arrays.asList;
import static java.util.regex.Pattern.CASE_INSENSITIVE;
import static java.util.regex.Pattern.quote;

public class StringUtility
{
    public static String getString(InputStream is)
    {
        Scanner s = new Scanner(is).useDelimiter("\\A");
        return s.hasNext() ? s.next() : "";
    }

    public static String stripTags(String input, String allowedTags)
    {
        List<String> knownTagList = asList(allowedTags.replaceAll("^<", "").replaceAll(">$", "").split("><"));
        Pattern tag = Pattern.compile("</?([^\\s>]*)\\s*[^>]*>", CASE_INSENSITIVE);
        Matcher matches = tag.matcher(input);
        while (matches.find())
        {
            if (!knownTagList.contains(matches.group(1)))
            {
                input = input.replaceAll(quote(matches.group()), "");
            }
        }
        return input;
    }
}
